package org.puremvc.as3.demos.flex.cafetownsend.model
{
	import com.salesforce.AsyncResponder;
	import com.salesforce.Connection;
	import com.salesforce.objects.LoginRequest;
	import com.salesforce.results.LoginResult;
	import com.salesforce.results.QueryResult;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	
	import org.puremvc.as3.demos.flex.cafetownsend.ApplicationFacade;
	import org.puremvc.as3.demos.flex.cafetownsend.model.business.LoadContactsDelegate;
	import org.puremvc.as3.demos.flex.cafetownsend.view.EmployeeLoginMediator;
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;

	public class ContactsProxy extends Proxy implements IProxy
	{

		private var force:Connection = new Connection();
		private var lr:LoginRequest = new LoginRequest();
		
		public var empLogMed:EmployeeLoginMediator;
		
		public var cList:ArrayCollection = new ArrayCollection();
		
		public var loadContactsDelegate:LoadContactsDelegate;
		
		public function ContactsProxy()
		{
		}
		
		public function loginToSalesForce(uname:String, pwd:String):void {
			if(uname != null && pwd != null)
			{
				var lr:LoginRequest = new LoginRequest();
				lr.username = uname;
				lr.password = pwd+"FPR6R4JTrGi4TCWHIPYI3XBZ";
				lr.callback = new AsyncResponder(loginSuccess);
				force.login(lr);
			}
			
		}
		
		private function loginSuccess(result:LoginResult):void
		{
			//sendNotification(ApplicationFacade.VIEW_EMPLOYEE_LIST);
			Alert.show("Login Success");
			force.query("SELECT CreatedDate,Email,FirstName,LastName FROM Contact", new AsyncResponder(queryResult));
			//var loadContacts:LoadContactsDelegate = new LoadContactsDelegate();
			//loadContacts.getAllContacts(result);
			//data = loadContactsDelegate.cList;
			
		}
		
		public function queryResult(result:QueryResult):void {
			//cList = result.records;
			data = result.records;
			Alert.show("Contacts:"+result.records.length);
			//empProxy.contsList = cList;
			//Alert.show("Contacts List in emp login mediator:"+empProxy.contsList.length);
			//employeeList.dataGrid.dataProvider = cList;
			sendNotification(ApplicationFacade.VIEW_EMPLOYEE_LIST);
		}
		
		public function get contactsListDP():ArrayCollection
		{
			
			//Alert.show("contsList in EmpProxy="+contsList.length);
			return data as ArrayCollection;
		}
		
	}
}